package src.leetcode.greedy.hard_0968;


import src.tree.TreeNode;

import java.util.ArrayList;
import java.util.Arrays;

public class minCameraCover {
    public static void main(String[] args) {
        Solution solution = new Solution();
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(0));
        TreeNode root = TreeNode.createTree(list);
        System.out.println(solution.minCameraCover(root));
    }
}


class Solution {
    int count = 0;
    public int minCameraCover(TreeNode root) {
        if(dfs(root) == 1) count++;
        return count;
    }

    private int dfs(TreeNode root){
        if(root == null) return 0;

        int left = dfs(root.left);
        int right = dfs(root.right);

        if(left == 0 && right == 0) return 1;//叶子节点，上一层需要监控

        if(left == 1 || right == 1) {
            count++;
            return 2;
        }

        return 0;
    }
}

